package com.yd.mvc.mobile.orderController;

import com.yd.app.service.mobile.orderService.MOrderService;
import com.yd.app.service.pc.userCartService.UserCartService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Order;
import com.yd.domain.business.OrderDetial;
import com.yd.domain.business.OrderGoods;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.MessageReturnParam;
import com.yd.domain.searchParam.OrderParam;
import com.yd.domain.sys.DicEnum;
import com.yd.domain.sys.PermEnum;
import com.yd.domain.sys.User;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 提交订单
 * Created by sunxianhui on 2018/3/7.
 */
@RestController
@RequestMapping(value = "/mapi/morder")
public class MOrderController extends BaseController {
    @Autowired
    private MOrderService orderService;
    @Autowired
    private UserCartService userCartService;
    /**
     * 添加新订单
     * @param orderDetial
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Order addOrder(@RequestBody OrderDetial orderDetial, HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        Order order=null;
        if(u!=null){
            order=orderService.addOrderDetail(orderDetial,u.getId(),u.getCompanyName(),u.getRealName());
        }else{
            throw new BusinessException(401,"尚未登录","no-login");
        }
        return order;
    }

    /**
     * 修改
     * @param order
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateOrder(@RequestBody Order order) {
        orderService.updateOrder(order);
    }

    /**
     * 删除
     * @param id
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public void deleteOrder(@PathVariable("id") String id, HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        if(u!=null){
            if(u.getPermCodeList().contains(PermEnum.ORDER_MANAGER)) {//是否具有订单管理权限
                orderService.deleteOrderByid(id,PermEnum.ORDER_MANAGER);
            }else{
                orderService.deleteOrderByid(id,"null");
            }
        }else{
            throw new BusinessException(401,"尚未登录","no-login");
        }
    }

    /**
     * 根据id查询
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/getOrder/{orderId}", method = RequestMethod.GET)
    public Order getOrder(@PathVariable("orderId") String orderId) {
        Order order = orderService.selectOrderById(orderId);
        return order;
    }

    /**
     * 根据条件分页查询
     * @param orderParam
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getList/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getOrderList(@RequestBody OrderParam orderParam, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize,HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        PageModel pageModel = null;
        if(u!=null){
            try {
                if(!u.getPermCodeList().contains(PermEnum.ORDER_MANAGER)) {//是否具有订单管理权限
                    orderParam.setUserId(u.getId());
                }
                pageModel = orderService.getOrderList(orderParam, pageIndex, pageSize);
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException(500, "获取订单信息失败");
            }
        }
        return pageModel;
    }
    /**
     * @Author: zy
     * @Desc: 一键更新
     * @Date:  2018/4/27
     * @Param
     */
    @RequestMapping(value = "/updatePrice", method = RequestMethod.POST)
    public void updatePrice(@RequestBody Order order,HttpSession session)throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        if(u!=null){
            if(u.getPermCodeList()!=null){
                if(u.getPermCodeList().contains(PermEnum.ORDER_MANAGER)) {//是否具有订单管理权限
                    orderService.updatePrice(order);
                }else{
                    throw new BusinessException(500, "权限不足");
                }
            }
        }else{
            throw new BusinessException(401,"尚未登录");
        }
    }
    /**
     * 根据orderId订单状态
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/reviewOrder/{orderId}/{status}", method = RequestMethod.GET)
    public void reviewOrder(@PathVariable("orderId") String orderId,@PathVariable("status") String status,HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        if(u!=null){
            if(u.getPermCodeList()!=null){
                if(u.getPermCodeList().contains(PermEnum.ORDER_MANAGER)) {//是否具有订单管理权限
                    orderService.reviewOrder(orderId,status);
                }else{
                    throw new BusinessException(500, "权限不足");
                }
            }
        }else{
            throw new BusinessException(401,"尚未登录");
        }
    }

    /**
     * 付款状态
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/payStatus/{orderId}/{payStatus}", method = RequestMethod.GET)
    public void payStatus(@PathVariable("orderId") String orderId,@PathVariable("payStatus") String payStatus,HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        if(u!=null){
            if(u.getPermCodeList()!=null){
                if(u.getPermCodeList().contains(PermEnum.ORDER_MANAGER)) {//是否具有订单管理权限
                    orderService.payStatus(orderId,payStatus);
                }else{
                    throw new BusinessException(500, "权限不足");
                }
            }
        }
    }

    /**
     * 添加/修改物流快递单号
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/expressCode/{orderId}/{expressCode}", method = RequestMethod.GET)
    public void expressCode(@PathVariable("orderId") String orderId,@PathVariable("expressCode") String expressCode) {
        orderService.expressCode(orderId,expressCode);
    }

    /**
     * 修改配货状态
     * @param goods
     */
    @RequestMapping(value = "/updatePhStatus", method = RequestMethod.PUT)
    public void updatePhStatus(@RequestBody OrderGoods goods) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.ORDER_MANAGER);//验证权限
        }
        orderService.updatePhStatus(goods);
    }
}
